package moni.anyou.com.view.view.addresslist;;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import moni.anyou.com.view.R;
import moni.anyou.com.view.base.BaseActivity;
import moni.anyou.com.view.view.fragmentadapter.FragmentAdapter;

import moni.anyou.com.view.widget.utils.view.ViewUtil;

public class AddressListActivity extends BaseActivity {
	private FamilyFragment familyFragment;
	private CollegeFragment collegeFragment;

	private FragmentAdapter mFragmentAdapter;
	private HorizontalScrollView title_view;
	ViewUtil viewUtil;

	private List<Fragment> fragmentList = new ArrayList<Fragment>();
	private RadioGroup mRadioGroup;
	ViewPager mViewPager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_list);
		init();
	}

	@Override
	public void initView() {
		super.initView();
		viewUtil = new ViewUtil(this);
		mRadioGroup = (RadioGroup) findViewById(R.id.title_group);
		mViewPager = (ViewPager) findViewById(R.id.vp_order_form);
		title_view = (HorizontalScrollView) findViewById(R.id.title_view);
		viewUtil.setViewWidth(title_view,viewUtil.getScreenWidth()*2/3);
		mFragmentAdapter = new FragmentAdapter(mBaseActivity.getSupportFragmentManager());

		mViewPager.setOffscreenPageLimit(mRadioGroup.getChildCount());
		for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
			RadioButton itemView = (RadioButton) mRadioGroup.getChildAt(i);
			viewUtil.setViewWidth(itemView, viewUtil.getScreenWidth() /3);
		}
		familyFragment = new  FamilyFragment();
		familyFragment.setTypeid(2);
		collegeFragment = new CollegeFragment();
		collegeFragment.setTypeid(1);
		fragmentList.add(familyFragment );
		fragmentList.add(collegeFragment);
		((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);
		mFragmentAdapter.setList(fragmentList);
		mViewPager.setAdapter(mFragmentAdapter);
		mViewPager.addOnPageChangeListener(mOnPageChangeListener);
		mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				int currentItem = 0;
				for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
					if (checkedId == mRadioGroup.getChildAt(i).getId()) {
						currentItem = i;
					}
				}
				if (currentItem < 3) {
					title_view.scrollTo(
							(0) * viewUtil.getScreenWidth() / 2, 0);
				} else {
					title_view.scrollTo(
							(7) * viewUtil.getScreenWidth() / 2, 0);
				}
				mViewPager.setCurrentItem(currentItem, false);
			}
		});
	}


	private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			RadioButton selectButton=   ((RadioButton)mRadioGroup.getChildAt(0));
			RadioButton selectButton2=   ((RadioButton)mRadioGroup.getChildAt(1));
			if (arg0 == 0) {

				selectButton.setChecked(true);
				selectButton.setTextSize(16);
				selectButton2.setTextSize(16);
				familyFragment.getdata();

			} else {
				// publicVidioFragement.onAttach(getApplicationContext());
				selectButton2.setChecked(true);
				selectButton.setTextSize(16);
				selectButton2.setTextSize(16);
				collegeFragment.getdata();

			}

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	};

}
